import {
  db
} from '../mongoDB/index.js';
import {
  COLLECTION
} from '../mongoDB/default.js';
import exportExcel from '../exportExcel/index.js';

const paramsFun = (params) => {
  // 判断参数中是否存在用户id
  if (!params.userId) {
    return {
      errorCode: 402,
      msg: '参数错误',
      data: {
        params: params
      }
    }
  }
  return undefined
}

const hasUserInDB = async (params) => {
  const collection = db.collection(COLLECTION.USER);

  // 判断数据库中是否存在该用户
  const result = await collection.find({
    userId: params.userId
  }, {}).toArray();

  if (!result.length) {
    return {
      errorCode: 2001,
      msg: '用户ID不存在',
      data: {
        params: params
      }
    }
  }
  return undefined;
}

// 判断用户是否存在
const hasUser = async (params) => {
  // 判断参数中是否存在用户id
  if (!params.userId) {
    return {
      errorCode: 402,
      msg: '参数错误',
      data: {
        params: params
      }
    }
  }

  const collection = db.collection(COLLECTION.USER);

  // 判断数据库中是否存在该用户
  const result = await collection.find({
    userId: params.userId
  }, {}).toArray();

  if (!result.length) {
    return {
      errorCode: 2001,
      msg: '用户ID不存在',
      data: {
        params: params
      }
    }
  }
}

// 查询用户列表信息
const getUser = async () => {
  try {
    // 查询数据集合user的所有数据并转为数组形式
    const collection = await db.collection(COLLECTION.USER).find({}).toArray();
    return {
      code: 200,
      data: {
        list: collection,
        total: collection.length || 0,
      },
    }
  } catch (err) {
    return {
      errorCode: 1002,
      data: {},
      msg: '数据库错误'
    }
  }
}

// 添加单个用户
const addUser = async (params) => {
  const keys = Object.keys(params);

  if (!keys.length) {
    return {
      errorCode: 402,
      data: '',
      msg: '参数错误'
    }
  }

  const userInfo = await hasUserInDB(params);

  if (!userInfo) {
    return {
      errorCode: 2001,
      msg: '用户已存在，不可重复添加',
      data: {}
    }
  }

  const collection = db.collection(COLLECTION.USER);
  //单个插入
  const insertResult = await collection.insertOne(params);

  const flag = insertResult.acknowledged;

  return {
    // code: 200,
    errorCode: flag ? 200 : 1002,
    data: {
      params: params
    },
    msg: flag ? 'success' : '数据库错误'
  }
}

// 编辑单个用户信息
const editUser = async (params) => {

  // data存在说明用户不存在
  const data = await hasUser(params);
  if (data) {
    return data
  }
  const collection = db.collection(COLLECTION.USER);

  collection.updateOne({
    userId: params.userId
  }, {
    "$set": {
      ...params,
    }
  })

  return {
    data: {}
  }
}

// 删除单个用户
const delUser = async (params) => {

  // data存在说明用户不存在
  const data = await hasUser(params);
  if (data) {
    return data
  }

  const collection = db.collection(COLLECTION.USER);

  // 执行删除
  // collection.deleteMany({
  //   userId: params.userId
  // })
  collection.deleteOne({
    userId: params.userId
  })

  //删除集合中的所有信息命令
  // collection.deleteMany({});

  return {}
}

// 批量添加用户
const addUserBitch = async () => {

}

const delUserBitch = async () => {

}

// Todo：导入xlxs文件，传入数据进用户表
const importUser = async (params) => {
  console.log('params',params);
  return {
    data: {}
  }
}

// 批量导出用户信息
const exportUser = async () => {
  // 查询数据集合user的所有数据并转为数组形式
  const collection = await db.collection(COLLECTION.USER).find({}).toArray();
  const columns = [{
      header: '用户编号',
      width: 20,
      key: 'userId',
    },
    {
      header: '姓名',
      width: 20,
      key: 'name',
    },
    {
      header: '邮箱',
      width: 20,
      key: 'email',
    },
    {
      header: '所属角色',
      width: 20,
      key: 'roleId',
    }
  ]
  const data = await exportExcel(collection, columns, '');

  return {
    code: 200,
    data: data,
    responseHeader: {
      "Content-Disposition": `attachment; filename="用户信息.xlsx"`,
      "Content-Type": 'application/vnd.ms-excel; charset=utf-8'
    }
    // contentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;fileName=用户信息.xlsx',
  }
}

export default {
  getUser,
  addUser,
  editUser,
  delUser,
  addUserBitch,
  delUserBitch,
  importUser,
  exportUser,
}